package com.lovo.controller;

import com.github.pagehelper.PageInfo;
import com.lovo.VO.RoleVO;
import com.lovo.model.Role;
import com.lovo.service.RoleService;
import com.lovo.service.impl.RoleServiceImpl;
import com.lovo.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "RoleServlet",urlPatterns = "/role.do")
public class RoleServlet extends HttpServlet {
    RoleService rs = new RoleServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if (method == null || method.equalsIgnoreCase("")){
            indexByPage(request,response);
        }else if (method.equalsIgnoreCase("addRole")){
            addRole(request,response);
        }else if (method.equalsIgnoreCase("deleteRole")){
            deleteRole(request,response);
        }else if (method.equalsIgnoreCase("updateRole")){
            updateRole(request,response);
        }else if (method.equalsIgnoreCase("hasAuthority")){
            hasAuthority(request,response);
        }else if (method.equalsIgnoreCase("getAuthority")){
            getAuthority(request,response);
        }else if (method.equalsIgnoreCase("getAllRoles")){
            getAllRoles(request,response);
        }

    }

    private void getAllRoles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        List<Role> list = rs.getAllRoles();
        List<RoleVO>roleVOS = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int key = list.get(i).getRoleId();
            String label = list.get(i).getRoleName();
            RoleVO roleVO = new RoleVO();
            roleVO.setLabel(label);
            roleVO.setKey(key);
            roleVOS.add(roleVO);
        }
        out.write(MyUtils.toJSON(roleVOS));
    }

    private void getAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        int[] authorityIds= rs.getAuthorityIds(roleId);
//        String str = Arrays.toString(authorityIds);
        out.write(MyUtils.toJSON(authorityIds));
    }

    private void hasAuthority(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        String[] ids = request.getParameter("authorityIds").split(",");
        Integer[] authorityIds = new Integer[ids.length];
        for (int i = 0; i<ids.length;i++){
            authorityIds[i] = Integer.parseInt(ids[i]);
        }
        boolean flag = rs.hasAuthority(roleId,authorityIds);
        out.write(String.valueOf(flag));
    }

    private void updateRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        String roleName = request.getParameter("roleName");
        String roleDesc = request.getParameter("roleDesc");
        Role r = new Role();
        r.setRoleId(roleId);
        r.setRoleName(roleName);
        r.setRoleDesc(roleDesc);
        boolean flag = rs.updateRole(r);
        out.write(String.valueOf(flag));
    }

    private void deleteRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        boolean flag = rs.deleteRole(roleId);
        out.write(String.valueOf(flag));
    }

    private void addRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String roleName = request.getParameter("roleName");
        String roleDesc = request.getParameter("roleDesc");
        Role r = new Role();
        r.setRoleName(roleName);
        r.setRoleDesc(roleDesc);
        boolean flag = rs.addRole(r);
        out.write(String.valueOf(flag));
    }

    private void indexByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        int pageNo = 1;
        int pageSize = 10;
        String pageNo1 = request.getParameter("pageNo");
        String pageSize1 = request.getParameter("pageSize");
        if(pageNo1!=null&&!pageNo1.equals("")){
            pageNo = Integer.parseInt(pageNo1);
        }
        if(pageSize1!=null&&!pageSize1.equals("")){
            pageSize = Integer.parseInt(pageSize1);
        }

        String roleName = request.getParameter("roleName");
        String roleDesc = request.getParameter("roleDesc");
        Role r = new Role();
        r.setRoleName(roleName);
        r.setRoleDesc(roleDesc);
        PageInfo<Role> rolePageInfo = rs.listByPage(pageNo,pageSize,r);
        out.write(MyUtils.toJSON(rolePageInfo));
    }
}
